package com.bw.zg3park2212.park

import android.graphics.Color
import android.text.Editable
import android.text.TextWatcher
import android.view.View
import android.widget.AdapterView
import android.widget.AdapterView.OnItemSelectedListener
import android.widget.ArrayAdapter
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.ToastUtils
import com.bw.base.entity.ParkAddParam
import com.bw.base.entity.ParkTypeEntityItem
import com.bw.base.mvvm.BaseActivity
import com.bw.zg3park2212.R
import com.bw.zg3park2212.databinding.ActivityParkUpdetBinding
import dagger.hilt.android.AndroidEntryPoint

@Route(path ="/app/ParkUpdetActivity")
@AndroidEntryPoint
class ParkUpdetActivity : BaseActivity<ActivityParkUpdetBinding,ParkViewModel>() {
    var area=""
    var type=""
    var sataus=""
    @Autowired
    lateinit var bean:ParkTypeEntityItem
    override fun intitView() {
        ARouter.getInstance().inject(this)
        binding.edCwbh.setText(bean.spaceNumber)
        if (bean.spaceStatus=="空闲"){
            binding.rbKx.isChecked=true
            binding.rbZy.isChecked=false
            binding.rbWh.isChecked=false
        }else if (bean.spaceStatus=="已占用"){
            binding.rbKx.isChecked=false
            binding.rbZy.isChecked=true
            binding.rbWh.isChecked=false
        }else{
            binding.rbKx.isChecked=false
            binding.rbZy.isChecked=false
            binding.rbWh.isChecked=true
        }
        binding.edSyr.setText(bean.userName)
        binding.edCph.setText(bean.licensePlate)
        binding.parkaddRemark.setText(bean.remarks)
        binding.parkaddRemark.addTextChangedListener(object : TextWatcher{
            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {

            }

            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
                var leght=s?.length
                binding.parkaddNum.setText(""+leght+"/10")
                if (leght==10){
                    binding.parkaddNum.setTextColor(Color.RED)
                }else{
                    binding.parkaddNum.setTextColor(Color.GRAY)
                }
            }

            override fun afterTextChanged(s: Editable?) {

            }
        })
        var areas= mutableListOf<String>("A区停车场","B区停车场","C区停车场")
        var areasAdpter= ArrayAdapter(this,android.R.layout.simple_spinner_item,areas)
        areasAdpter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
        binding.edSsqy.adapter=areasAdpter
        binding.edSsqy.onItemSelectedListener=object:OnItemSelectedListener{
            override fun onItemSelected(
                parent: AdapterView<*>?,
                view: View?,
                position: Int,
                id: Long,
            ) {
                area=areas[position]
            }

            override fun onNothingSelected(parent: AdapterView<*>?) {

            }
        }
        if (!areas.contains(bean.areaName)){
            areas.add(0,bean.areaName)
        }else{
            binding.edSsqy.setSelection(areas.indexOf(bean.areaName))
        }
        var types= mutableListOf<String>("标准车位","VIP车位")
        var typesAdpter=ArrayAdapter(this,android.R.layout.simple_spinner_item,types)
        typesAdpter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
        binding.edCwlx.adapter=typesAdpter
        binding.edCwlx.onItemSelectedListener=object:OnItemSelectedListener{
            override fun onItemSelected(
                parent: AdapterView<*>?,
                view: View?,
                position: Int,
                id: Long,
            ) {
                type=types[position]
            }

            override fun onNothingSelected(parent: AdapterView<*>?) {

            }

        }
        if (!types.contains(bean.spaceType)){
            types.add(0,bean.spaceType)
        }else{
            binding.edCwlx.setSelection(types.indexOf(bean.spaceType))
        }
        binding.parkaddRg.setOnCheckedChangeListener { group, checkedId ->
            if (checkedId== R.id.rb_kx){
                sataus="空闲"
            }else if (checkedId== R.id.rb_zy){
                sataus="已占用"
            }else{
                sataus="维修中"
            }
        }
        binding.btnAdd.setOnClickListener {
            var parkAddParam=ParkAddParam()
            parkAddParam.spaceNumber=binding.edCwbh.text.toString()
            parkAddParam.areaId=4
            parkAddParam.spaceType=type
            parkAddParam.spaceStatus=sataus
            parkAddParam.userName=binding.edSyr.text.toString()
            parkAddParam.licensePlate=binding.edCph.text.toString()
            parkAddParam.remarks=binding.parkaddRemark.text.toString()
            viewmodel.parkup(bean.id,parkAddParam)
        }
    }

    override fun intitData() {
        viewmodel.parkupSuccse.observe(this){
            ToastUtils.showLong("修改成功")
            finish()
        }
    }

}